﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Core.Primitives
{
    [Serializable]
    public class CircularArray<T>
    {
        private T[] m_array;
        public T[] Array
        {
            get
            {
                return m_array;
            }
        }

        public int Length
        {
            get
            {
                return m_array.Length;
            }
        }

        public CircularArray(int size)
        {
            m_array = new T[size];
        }
        public CircularArray(T[] array)
        {
            m_array = array;
        }
        public T this[int i]
        {
            get
            {
                return m_array[ConvertIndex(i)];
            }
            set
            {
                m_array[ConvertIndex(i)] = value;
            }
        }

        private int ConvertIndex(int i)
        {
            if (i < 0)
                i = i % m_array.Length + m_array.Length;

            if (i >= m_array.Length)
                i = i % m_array.Length;

            return i;
        }
    }
}
